﻿using HuiLian.Data.DataAccess.Base;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace HuiLian.Cloud.ViewModel.Base
{
    /// <summary>
    /// 数据库工具
    /// </summary>
    /// <typeparam name="Entity">泛型</typeparam>
    public class EntityHelper<Entity> : IDisposable where Entity : class, new()
    {
        private readonly EFContext Context = new EFContext();

        /// <summary>
        /// 增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Insert(params Entity[] entities)
        {
            foreach(Entity entity in entities)
            {
                Context.Entry<Entity>(entity).State = EntityState.Added;
            }
            return await Context.SaveChangesAsync();
        }

        /// <summary>
        /// 删
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Delete(params Entity[] entities)
        {
            foreach (Entity entity in entities)
            {
                Context.Entry<Entity>(entity).State = EntityState.Deleted;
            }
            return await Context.SaveChangesAsync();
        }

        /// <summary>
        /// 改
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> Update(params Entity[] entities)
        {
            foreach (Entity entity in entities)
            {
                Context.Entry<Entity>(entity).State = EntityState.Modified;
            }
            return await Context.SaveChangesAsync();
        }

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public async Task<Entity> Select(Expression<Func<Entity, bool>> where)
        {
            return await Context.Set<Entity>().Where(where).FirstOrDefaultAsync();
        }

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<Entity> GetEntities(Expression<Func<Entity, bool>> where = null)
        {
            IQueryable<Entity> result = Context.Set<Entity>();
            if (where != null) result = result.Where(where);
            return result;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageNo"></param>
        /// <param name="pageCount"></param>
        /// <param name="where"></param>
        /// <param name="select"></param>
        /// <param name="orderby"></param>
        /// <param name="total"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public Task<List<Entity>> PageQuery<T>(int pageNo, int pageCount, 
            Expression<Func<Entity, bool>> where,
            Expression<Func<Entity, Entity>> select, Expression<Func<Entity, T>> orderby, 
            out int total, bool isAsc = true)
        {
            int startIndex = pageCount * (pageNo - 1);
            var list = Context.Set<Entity>().Where(where);
            total = list.Count();
            if (total <= 0) return null;
            list = (isAsc ? list.OrderBy(orderby) : list.OrderByDescending(orderby));
            return list.Skip(startIndex).Take(pageCount).Select(select).ToListAsync();
        }

        /// <summary>
        /// 释放当前类内存
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// 释放数据库资源
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && Context.SaveChanges() > 0)
            {
                Context.Dispose();
            }
        }

    }
}
